//
//  SJCache.swift
//  Employee
//
//  Created by swordjoy on 2017/4/5.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON

struct SJCache {
  
  static func clearAll() {
    
    SJCache.clear()
    User.clearAll()
    Token.clearAll()
//    Account.clearAll()
    Cookie.clearAll()
    UIApplication.shared.applicationIconBadgeNumber = 0
    Account.isSetPushAlias = false
  }
  
  static func clear() {
    
    self.homeOrders = []
    self.homeInspections = []
    self.chatUsers = []
    
  }
  
  // MARK: - 首页工单缓存
  
  static let HomeOrderkey = "SJCache.HomeOrderkey"
  
  static var homeOrders: [WorkOrder] {
    get {
      
      if let data = GlobalConfig.userDefaults.object(forKey: SJCache.HomeOrderkey) {
        if let temps = NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as? [WorkOrder] {
          return temps
        }
      }
      
      return []
    }
    set {
      
      let data = NSKeyedArchiver.archivedData(withRootObject: newValue)
      GlobalConfig.userDefaults.set(data, forKey: SJCache.HomeOrderkey)
      
    }
  }
  

  // MARK: - 今日巡检缓存
  
  static let HomeInspectionkey = "SJCache.HomeInspectionkey"
  
  static var homeInspections: [Inspection] {
    get {
      if let data = GlobalConfig.userDefaults.object(forKey: SJCache.HomeInspectionkey) {
        if let temps = NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as? [Inspection] {
          return temps
        }
      }
      
      return []
    }
    set {
      
      let data = NSKeyedArchiver.archivedData(withRootObject: newValue)
      GlobalConfig.userDefaults.set(data, forKey: SJCache.HomeInspectionkey)
      
    }
  }
  
  // // MARK: - 通讯录缓存
  
  static let AddressBookKey = "SJCache.AddressBookkey"
  
  static var chatUsers: [ChatUser] {
    get {
      if let data = GlobalConfig.userDefaults.object(forKey: SJCache.AddressBookKey) {
        if let temps = NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as? [ChatUser] {
          return temps
        }
      }
      
      return []
    }
    set {
      let data = NSKeyedArchiver.archivedData(withRootObject: newValue)
      GlobalConfig.userDefaults.set(data, forKey: SJCache.AddressBookKey)
    }
  }

  
}
